﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace PDB
{
    class Program
    {
        static void Main(string[] args)
        {
            var buffer = new byte[0xFFFF];
            for (var i = 0; i < buffer.Length; i++)
                buffer[i] = (byte) (i & 0xFF);



            //var book = new Book(@"C:\09G9.pdb");
            //Console.ReadLine();
            //return;

        }

        static void Show(Stream stream, int offset, int length)
        {
            stream.Seek(offset, SeekOrigin.Begin);

            var buffer = new byte[length];
            var len = stream.Read(buffer, 0, length);
            var s = Encoding.Default.GetString(buffer, 0, len);
            Console.Clear();
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }

    class Book
    {
        private Encoding _enc;
        public string FilePath { get; private set; }
        public int Count { get; private set; }
        public readonly int[] Offsets;
        public Book(string filePath)
        {
            if (!File.Exists(filePath))
                throw new FileNotFoundException();
            FilePath = filePath;

            using (var f = File.OpenRead(filePath))
            {
                var buffer = new byte[78];
                f.Read(buffer, 0, 78);
                var type = Encoding.ASCII.GetString(buffer, 60, 8);
                Count = ToInt(buffer, 76, 2);
                switch (type)
                {
                    case "BOOKMTIT": _enc = Encoding.Default; break;
                    case "BOOKMTIU": _enc = Encoding.UTF32; break;
                }
                Offsets = new int[Count+2];
                for (var i = 0; i < Count + 2; i++)
                {
                    f.Read(buffer, 0, 8);
                    Offsets[i] = ToInt(buffer, 0, 4);
                }
            }
        }
        private static int ToInt(IList<byte> buffer, int offset, int length)
        {
            var n = 0;
            for (var i = 0; i < length; i++)
                n += buffer[offset + i] << ((length - i - 1)*8);
            return n;
        }
    }

}
